package com.tansuo365.test1.service.otherservice.impl;

import com.tansuo365.test1.bean.otherbean.YanshouDetails;
import com.tansuo365.test1.bean.otherbean.YanshouZong;
import com.tansuo365.test1.mapper.othermapper.YanshouDetailsMapper;
import com.tansuo365.test1.mapper.othermapper.YanshouZongMapper;
import com.tansuo365.test1.service.otherservice.YanshouZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * zongshiyouhenduohuaxiangduinishuo
 * danshichawanyanshouzongbiaohouwokabhujianxiangxi
 * mingmingdoushuoxiangxibuxuyaotaixiangxi
 * kezongshizaiwoxintouyinyinganjuehenzhongyao
 */
@Service
public class YanshouZongServiceImpl implements YanshouZongService {
    @Resource
    private YanshouZongMapper yanshouZongMapper;
    @Resource
    private YanshouDetailsMapper yanshouDetailsMapper;

    @Override
    public String getTodayMaxYanshouDanNumber() {
        return yanshouZongMapper.getTodayMaxYanshouDanNumber();
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return yanshouZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(YanshouZong yanshouZong) {
        return yanshouZongMapper.insertSelective(yanshouZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(YanshouZong yanshouZong, List<YanshouDetails> yanshouDetailsList) {
        int insertCode = 0;
        insertCode = yanshouZongMapper.insertSelective(yanshouZong);
        if(!(insertCode>0)){
            return 0;
        }
        int i = 0;
        int zongzhangId = yanshouZong.getId();
        int deInsertCode = 0;
        //保存每个验收单物料
        for(YanshouDetails yanshouDetails : yanshouDetailsList){
            i++;
            yanshouDetails.setZongzhangid(zongzhangId);
            deInsertCode += yanshouDetailsMapper.insertSelective(yanshouDetails);
        }
        if(!(i==yanshouDetailsList.size() && i!=0) || !(deInsertCode>0)){
            return 0;
        }
        if(deInsertCode!=i){
            return -1;
        }
        return i;
    }

    @Override
    public YanshouZong selectByPrimaryKey(String id) {
        return yanshouZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YanshouZong yanshouZong) {
        return yanshouZongMapper.updateByPrimaryKeySelective(yanshouZong);
    }

    @Override
    public List<YanshouZong> listYanshouZong(YanshouZong yanshouZong) {
        return yanshouZongMapper.listYanshouZong(yanshouZong);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return yanshouZongMapper.deleteBatchByPKArr(ids);
    }

}
